/* $RCSFile: EditPortal.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/PortalsAdmin/EditPortal.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:22  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:58  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Design;
using AddonNice.UI;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.PortalsAdmin
{
    /// <summary>
    /// EditPortal
    /// </summary>
	public partial class EditPortal : EditItemPage
	{
        PortalsDB pDB   =   new PortalsDB();

        int currentPortalID
        {
            get
            {
                object ob   =   Request.Params["portalID"];
                if ( ob != null )
                    return Int32.Parse(ob.ToString());
                return -1;
            }
        }


        private void Page_Load(object sender, System.EventArgs e)
        {
            if (SettingsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditPortal Page_Load currentPortalID: {0}", currentPortalID),SettingsTraceSwitch.Sw.Info);
            // Verify that the current user has access to this page
            if ( !PortalSecurity.IsUserInRoles(Config.CRMAdminRole) || currentPortalID == -1 ) 
            {
                PortalSecurity.AccessDeniedEdit();
            }


            // If this is the first visit to the page, populate the site data
            if ( !IsPostBack ) 
            {
                // Obtain PortalSettings of this Portal
                PortalSettings currentPortalSettings    = ( portalSettings.PortalID ==  currentPortalID) ? portalSettings:new PortalSettings(currentPortalID);
                PortalIDField.Text                      =   currentPortalID.ToString();
                TitleField.Text                         =   currentPortalSettings.PortalName;
                TbAlias.Text                            =   currentPortalSettings.BaseAlias;
                PathField.Text                          =   currentPortalSettings.PortalPath;
                TbCrmServer.Text                        =   currentPortalSettings.CrmServer.DiscoServer;
                TbCrmOrganization.Text                  =   currentPortalSettings.CrmServer.Organization;
                CbType.Items[0].Selected                =   (currentPortalSettings.PortalType & PortalType.MultiCrmServer) != 0;
                CbType.Items[1].Selected                =   (currentPortalSettings.PortalType & PortalType.MultiOrganization) != 0;

                EditTable.DataBind();// Bind only on first pass
            }
        }
 
		/// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
				al.Add ("366C247D-4CFB-451D-A7AE-649C83B05841");
				return al;
			}
		}
	
		/// <summary>
        /// Update Portal data
        /// </summary>
        void EditPortal_Update(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
			try
			{
                
                int ptype           =   (CbType.Items[0].Selected) ? 1:0;
                if (CbType.Items[1].Selected) 
                    ptype           |=  2;
                pDB.UpdatePortal2(currentPortalID, TitleField.Text.Trim(),PathField.Text.Trim(),ptype);
				// Update custom settings in the database
				EditTable.ObjectID  =   currentPortalID;
				EditTable.UpdateControls();
			}
			catch(Exception ex)
			{
                string strErr           =   LocalizeHelper.Localize("EDIT_PORTAL_ERROR", "An error occurred when saving portal settings");
				if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditPortal EditPortal_Update, {0}, ex : {1}",strErr,ex),PageTraceSwitch.Sw.Info);

				ErrorMessage.Visible    =   true;
				ErrorMessage.Text       =   strErr + "<br/>";
                e.Cancel                =   true;
				return;
			}
        }

		private void EditTable_UpdateControl(object sender, SettingsTableItemEventArgs e)
		{
            SettingsTable edt = (SettingsTable) sender;
            if (SettingsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditPortal EditTable_UpdateControl e.CurrentItem.EditControl.ID: {0}, e.CurrentItem.Value: [{1}]", e.CurrentItem.EditControl.ID, e.CurrentItem.Value),SettingsTraceSwitch.Sw.Info);
			try
			{
				pDB.UpdatePortalSetting(edt.ObjectID, e.CurrentItem.EditControl.ID, e.CurrentItem.Value);
			}
			catch (Exception ex)
			{
				if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditPortal EditTable_UpdateControl ex : {0}",ex),PageTraceSwitch.Sw.Info);
			}
		}

		#region Std Overrides

		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();

			//Translations
			RequiredTitle.ErrorMessage = LocalizeHelper.Localize("VALID_FIELD","You must enter a name");
		
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() 
		{
			EditTable.UpdateControl +=  new UpdateControlEventHandler(EditTable_UpdateControl);
			Load                    +=  new System.EventHandler(this.Page_Load);
            Update                  +=  new PageEventHandler(EditPortal_Update);
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get
			{ return PlaceHolderButtons; }
			set { }
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get
			{ return PlaceHolderButtons2; }
			set { }
		}

        #endregion

    }
}

